import { parseTime } from '@/utils';

const formatJson = (filterVal, jsonData) => {
    return jsonData.map(v =>
        filterVal.map(j => {
            if (j === 'timestamp') {
                // 因为excel时间有出入 需要转化
                return parseTime(v[j]);
            } else {
                return v[j];
            }
        })
    );
};

/**
 * 导出
 * @param tHeader
 * @param filterVal
 * @param list
 * @param filename
 * @param bookType
 */
export const exportFile = (
    tHeader,
    filterVal,
    list,
    filename,
    bookType
) => {
    return new Promise(resolve => {
        filename = filename || '导出文件';
        bookType = bookType || 'xlsx';
        import('@/utils/Export2Excel').then(excel => {
            const data = formatJson(filterVal, list);
            excel.export_json_to_excel({
                header: tHeader,
                data,
                filename,
                autoWidth: true,
                bookType // 'xlsx' 'csv' 'txt'
            });
            resolve();
        });
    })

};
